//
//  ST_RecommendPecVideoTransition.swift
//  btvideo
//
//  Created by mac on 2020/6/9.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_RecommendPecVideoTransition: NSObject,UIViewControllerTransitioningDelegate{

    fileprivate var startRect = CGRect.zero
    
    fileprivate var coverURL:URL?
    
    fileprivate var isFullScreen = true
    
    init(startFrame:CGRect,isFullScreen:Bool = true,url:URL?) {
        
        super.init()
        
        startRect = startFrame
        
        coverURL = url
        
        self.isFullScreen = isFullScreen
    }
    
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
     
        
        return ST_RecommendVideoAnimator.init(startFrame: self.startRect,isFullScreen: self.isFullScreen, url: self.coverURL)
        
    }
    
}



//MARK: - UIViewControllerAnimatedTransitioning
class ST_RecommendVideoAnimator: NSObject,UIViewControllerAnimatedTransitioning{
 
    
    fileprivate var startRect = CGRect.zero
    
    fileprivate var coverURL:URL?
    
    fileprivate var isFullScreen = true
    
    init(startFrame:CGRect,isFullScreen:Bool = true,url:URL?) {
        
        super.init()
        
        startRect = startFrame
        
        coverURL = url
        
        self.isFullScreen = isFullScreen
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        
        guard let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? ST_RecommendPecVideoController else{
            return
        }
        
        let container = transitionContext.containerView
        
        toVC.view.alpha = 0
        
        container.addSubview(toVC.view)
        
        let animate_image_view = UIImageView.init(frame: self.startRect)
        
        animate_image_view.backgroundColor = .black
        
        if self.isFullScreen{
            animate_image_view.contentMode = UIView.ContentMode.scaleAspectFill
        }else{
            animate_image_view.contentMode = UIView.ContentMode.scaleAspectFit
        }
        
        
        animate_image_view.pin_setImage(from: self.coverURL, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        let endRect = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        
        container.addSubview(animate_image_view)
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            
            animate_image_view.frame = endRect
            
        }) { (f) in
            
            if let idx = ST_RecommendVideoDataAdapter.shared.curr_selected_index{
                
                toVC.normalIdx = idx
            }
            
            toVC.view.alpha = 1
            animate_image_view.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
        
        
    }
    

    
    
    
}
